﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.WindowsCE.Forms;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace TTNaviBack
{
    public partial class Form_NaviBack : Form
    {

        /*
        [DllImport("coredll.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        public static extern int PostMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
        public const int WM_USER = 0x400;
        public const int REMSG_SETTOP = WM_USER + 9100;
        */
        Thread m_thdRecv;
        UdpClient m_udpRecv;
        //IntPtr m_nWnd;
        bool m_bSetTopMost = false;
        public Form_NaviBack(string[] args)
        {
            //m_nWnd = new IntPtr(System.Convert.ToInt32(args[0]));
            InitializeComponent();
            this.timer_Check.Enabled = true;
            //this.button_NaviBack.Text += ":" + m_nWnd.ToString();
            m_bSetTopMost = true;
            m_thdRecv = new Thread(new ThreadStart(doRecv));
            m_thdRecv.Start();
        }

        private void button_ExitNavi_Click(object sender, EventArgs e)
        {

        }
        private void doRecv()
        {
            m_udpRecv = new UdpClient(60001);
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

            while (true)
            {
                try
                {

                    byte[] bReceive = m_udpRecv.Receive(ref RemoteIpEndPoint);

                    if (System.Text.Encoding.Default.GetString(bReceive, 0, bReceive.Length) == "EOK")
                    {
                        m_bSetTopMost = true;
                       // MessageBox.Show("RECV");
                    }
                    if (System.Text.Encoding.Default.GetString(bReceive, 0, bReceive.Length) == "ENK")
                    {
                        m_bSetTopMost = false;
                        // MessageBox.Show("RECV");
                    }

                }
                catch (Exception ex)
                {

                }
                Thread.Sleep(1000);
            }
        }
        private void button_NaviBack_Click(object sender, EventArgs e)
        {

            try
            {

                //Message msg = Message.Create(m_nWnd, REMSG_SETTOP, new IntPtr(0), new IntPtr(0));
                //MessageWindow.SendMessage(ref msg);
                //PostMessage(m_nWnd, REMSG_SETTOP, new IntPtr(0), new IntPtr(0));
                m_nSendCount = 3;
                m_bSetTopMost = false;
                /*UdpClient uc = new UdpClient();
                uc.Send(System.Text.Encoding.Default.GetBytes("EOK"), 3,"localhost", 60000);
                //System.Threading.Thread.Sleep(3000);
                //uc.Send(System.Text.Encoding.Default.GetBytes("EOK"), 3, "localhost", 60000);
                uc.Close();*/
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

            //this.Close();
        }

        private void timer_Check_Tick(object sender, EventArgs e)
        {
            //if (this.TopMost != m_bSetTopMost)
            {
                this.TopMost = m_bSetTopMost;
            }
            this.Refresh();
            if (!m_bSetTopMost)
            {
                if (m_nSendCount > 0)
                {
                    m_nSendCount -= 1;
                    try
                    {

                        //Message msg = Message.Create(m_nWnd, REMSG_SETTOP, new IntPtr(0), new IntPtr(0));
                        //MessageWindow.SendMessage(ref msg);
                        //PostMessage(m_nWnd, REMSG_SETTOP, new IntPtr(0), new IntPtr(0));
                        m_bSetTopMost = false;
                        UdpClient uc = new UdpClient();
                        uc.Send(System.Text.Encoding.Default.GetBytes("EOK"), 3, "localhost", 60000);
                        //System.Threading.Thread.Sleep(3000);
                        //uc.Send(System.Text.Encoding.Default.GetBytes("EOK"), 3, "localhost", 60000);
                        uc.Close();
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        int m_nSendCount = 0;

        private void Form_NaviBack_Closing(object sender, CancelEventArgs e)
        {
            try
            {
                m_thdRecv.Abort();
            }
            catch (Exception ex)
            {
            }

            try
            {
                m_udpRecv.Close();
            }
            catch (Exception ex)
            {
            }
        }
    }
}